package com.ec50n9.server.module.issueCategory

import com.ec50n9.server.module.common.Render
import com.ec50n9.server.module.common.Rendered
import org.hibernate.annotations.GenericGenerator
import javax.persistence.*
import javax.validation.constraints.NotBlank

@Entity
class IssueCategory(
    var name: String,
    @Column(unique = true)
    var code: String,
    @OneToMany var childrens: Set<IssueCategory>? = null,

    @Id
    @GenericGenerator(name = "nanoid", strategy = "com.ec50n9.server.common.NanoIdGenerator")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "nanoid")
    var id: String? = null
) : Render<IssueCategory>()

/**
 * 创建表单
 */
data class IssueCategoryCreateForm(
    @field: NotBlank(message = "分类名不能为空")
    var name: String? = null,
    @field: NotBlank(message = "分类代码不能为空")
    var code: String? = null
)

/**
 * 更新表单
 */
data class IssueCategoryUpdateForm(
    var name: String? = null,
    var code: String? = null
)

/**
 * 查询渲染
 */
data class IssueCategoryQueryRendered(
    var name: String? = null,
    var code: String? = null
) : Rendered<IssueCategory> {
    override fun renderFrom(from: IssueCategory) {
        name = from.name
        code = from.code
    }
}
